package com.koron.order.common.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.convertor.OrderConvertor;
import com.koron.order.common.bean.po.OrderDispatch;
import com.koron.order.common.bean.po.OrderDispatchWorker;
import com.koron.order.common.bean.po.OrderExecute;
import com.koron.order.common.bean.po.OrderExecutePart;
import com.koron.order.common.bean.vo.OrderExecuteVo;
import com.koron.order.common.mapper.OrderExecuteInfoMapper;
import com.koron.order.common.service.*;
import com.koron.order.defect.bean.vo.DefectExecuteVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName OrderExecuteInfoServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/9/23 17:52
 */
@Service
public class OrderExecuteInfoServiceImpl implements OrderExecuteInfoService {

    @Autowired
    OrderConvertor orderConvertor;

    @Autowired
    OrderExecuteService orderExecuteService;

    @Autowired
    OrderExecutePartService orderExecutePartService;

    @Autowired
    OrderDispatchService orderDispatchService;

    @Autowired
    OrderDispatchWorkerService orderDispatchWorkerService;

    @Override
    @TaskAnnotation("querySingleByExecuteId")
    public OrderExecuteVo querySingleByExecuteId(SessionFactory factory, String executeId) {
        OrderExecuteInfoMapper mapper = factory.getMapper(OrderExecuteInfoMapper.class);
        OrderExecuteVo orderExecuteVo = mapper.querySingleByExecuteId(executeId);
        return orderExecuteVo;
    }

    @Override
    @TaskAnnotation("queryListByOrderId")
    public List<OrderExecuteVo> queryListByOrderId(SessionFactory factory, String orderId) {
        OrderExecuteInfoMapper mapper = factory.getMapper(OrderExecuteInfoMapper.class);
        List<OrderExecuteVo> orderExecuteVos = mapper.queryListByOrderId(orderId);
        return orderExecuteVos;
    }

    @Override
    @TaskAnnotation("insertOrUpdateExecuteInfo")
    public String insertOrUpdateExecuteInfo(SessionFactory factory, OrderExecuteVo orderExecuteVo) {

        //转换成工单执行信息
        OrderExecute orderExecute = orderConvertor.voToOrderExecute(orderExecuteVo);

        String orderExecuteId = orderExecute.getId();
        String orderId = orderExecute.getOrderId();

        if (StringUtils.isEmpty(orderExecuteId)) {
            orderExecute.setId(CodeTools.getCode32());
            orderExecuteService.insert(factory, orderExecute);
        } else {
            orderExecuteService.update(factory, orderExecute);
        }

        //工单执行备件信息
        List<OrderExecutePart> orderExecutePart = orderExecuteVo.getOrderExecutePart();
        orderExecutePart.forEach(p -> {
            p.setExecuteId(orderExecute.getId());
            p.setOrderId(orderId);
        });

        orderExecutePartService.insertBatch(factory, orderExecutePart);

        return orderExecute.getId();
    }

    @Override
    @TaskAnnotation("querySubmitUserListByOrderId")
    public List<String> querySubmitUserListByOrderId(SessionFactory factory, String orderId, Boolean isLeader) {
        //根据工单id查询提交的人员  派工处设置好的


        OrderDispatch orderDispatch = orderDispatchService.queryByOrderId(factory, orderId);

        List<String> list = new ArrayList<>();

        if (!Objects.isNull(orderDispatch)) {
            if (orderDispatch.getIsOut() == 1) {
                list.add(orderDispatch.getOutLeader());
            }
            list.add(orderDispatch.getLeader());

        }

        //如果是只需要负责人,那么就此返回
        if (isLeader) {
            return list;
        }

        List<OrderDispatchWorker> orderDispatchWorkers = orderDispatchWorkerService.queryByOrderId(factory, orderId);

        if (!CollectionUtils.isEmpty(orderDispatchWorkers)) {
            orderDispatchWorkers.forEach(p -> {
                list.add(p.getWorker());
            });
        }

        return list;
    }

    @Override
    @TaskAnnotation("deleteExecuteInfo")
    public boolean deleteExecuteInfo(SessionFactory factory, String orderId, String orderExecuteId) {

        //根据工单id及工单执行id 删除执行信息
        orderExecuteService.deleteOrderExecute(factory, orderId, orderExecuteId);

        orderExecutePartService.deleteOrderExecutePart(factory, orderId, orderExecuteId);

        return true;
    }

    @Override
    @TaskAnnotation("recodePart")
    public Boolean recodePart(SessionFactory factory, List<DefectExecuteVO> defectExecuteVOList) {
        int result = -1;
        if (CollectionUtils.isEmpty(defectExecuteVOList)){
            return false;
        }
        for (DefectExecuteVO defectExecuteVO : defectExecuteVOList) {
            String orderType = defectExecuteVO.getOrderType();
            String orderId = defectExecuteVO.getOrderId();
            String executeId = defectExecuteVO.getExecuteId();
            List<OrderExecutePart> orderExecutePart = defectExecuteVO.getOrderExecutePart();
            if (!CollectionUtils.isEmpty(orderExecutePart)) {
                String finalExecuteId = executeId;
                orderExecutePart.forEach(e -> {
                    e.setExecuteId(finalExecuteId);
                    e.setOrderId(orderId);
                    e.setOrderType(orderType);
                });
            }
            result = orderExecutePartService.insertBatch(factory, orderExecutePart);
        }
        return result > 0;
    }

}
